<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <label class="el-form-item-label">名称</label>
        <el-input
          v-model="query.name"
          clearable
          placeholder="名称"
          style="width: 185px"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <label class="el-form-item-label">模型Key</label>
        <el-input
          v-model="query.key"
          clearable
          placeholder="模型的关键字"
          style="width: 185px"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <label class="el-form-item-label">流程类型</label>
        <el-select
          v-model="query.category"
          clearable
          filterable
          size="small"
          placeholder="流程类型"
          class="filter-item"
          style="width: 200px"
          @change="crud.toQuery"
        >
          <el-option
            v-for="item in categoryOptions"
            :key="item.categoryName"
            :label="item.categoryName"
            :value="item.categoryId"
          />
        </el-select>
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog
        :close-on-click-modal="false"
        :before-close="crud.cancelCU"
        :visible.sync="crud.status.cu > 0"
        :title="crud.status.title"
        width="500px"
      >
        <el-form
          ref="form"
          :model="form"
          :rules="rules"
          size="small"
          label-width="80px"
        >
          <el-form-item label="名称">
            <el-input v-model="form.name" style="width: 370px" />
          </el-form-item>
          <el-form-item label="流程类型" prop="category" style="width: 370px">
            <el-select
              v-model="cid"
              style="width: 370px"
              placeholder="请选择"
            >
              <el-option
                v-for="item in categoryOptions"
                :key="item.categoryName"
                :label="item.categoryName"
                :value="item.categoryId"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="模型Key">
            <el-input v-model="form.key" style="width: 370px" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button
            :loading="crud.cu === 2"
            type="primary"
            @click="crud.submitCU"
          >确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table
        ref="table"
        v-loading="crud.loading"
        :data="crud.data"
        size="small"
        style="width: 100%"
        @selection-change="crud.selectionChangeHandler"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column prop="category" label="流程类型">
          <template slot-scope="scope">
            <div>{{ scope.row.categoryName }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="id" label="模型ID" />
        <el-table-column prop="key" label="模型Key" />
        <el-table-column prop="name" label="名称" />
        <el-table-column prop="createTime" label="创建时间">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="lastUpdateTime" label="最新修改时间">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.lastUpdateTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="version" label="版本" />
        <el-table-column
          label="操作"
          width="160px"
          align="center"
          fixed="right"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              style="margin-left: -1px; margin-right: 2px"
              type="text"
              @click="designModel(scope.row)"
            >设计
            </el-button>
            <el-button
              v-permission="['admin', 'actReModel:edit']"
              style="margin-left: -2px"
              type="text"
              size="mini"
              @click="deploy(scope.row)"
            >部署</el-button>
            <el-button
              v-permission="['admin', 'actReModel:edit']"
              style="margin-left: -2px"
              type="text"
              size="mini"
              @click="downloadBpmn(scope.row)"
            >导出</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>
<script>
import { mapGetters } from 'vuex'
import crudActReModel from '@/api/workflow/actReModel'
import crudProcessCategory from '@/api/workflow/processCategory'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import pagination from '@crud/Pagination'

const defaultForm = {
  id: null,
  rev: null,
  name: null,
  key: null,
  category: null,
  createTime: null,
  lastUpdateTime: null,
  version: null,
  metaInfo: null,
  deploymentId: null,
  editorSourceValueId: null,
  editorSourceExtraValueId: null,
  tenantId: null
}
export default {
  name: 'ActReModel',
  components: { pagination, crudOperation, rrOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({
      title: '流程模型',
      url: 'api/actReModel',
      idField: 'id',
      sort: 'id,desc',
      crudMethod: { ...crudActReModel }
    })
  },
  data() {
    return {
      permission: {
        add: ['admin', 'actReModel:add'],
        edit: ['admin', 'actReModel:edit'],
        del: ['admin', 'actReModel:del']
      },
      rules: {
        id: [{ required: true, message: '主键不能为空', trigger: 'blur' }]
      },
      queryTypeOptions: [
        { key: 'name', display_name: '名称' },
        { key: 'key', display_name: '模型的关键字' },
        { key: 'category', display_name: '模型类别' }
      ],
      enabledTypeOptions: [
        { key: 'true', display_name: '正常' },
        { key: 'false', display_name: '禁用' }
      ],
      // 流程类型数据
      categoryOptions: [],
      // 流程分类ID
      cid: ''
    }
  },
  computed: {
    ...mapGetters({
      activitiModelApi: 'activitiModelApi'
    })
  },
  // 页面挂载时
  mounted: function() {
    this.getCategorys()
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    // 新增与编辑前做的操作
    [CRUD.HOOK.afterToCU](crud, form) {
      // 获取流程分类
      this.getCategorys()
    },
    // 新增前将选项的值值为空
    [CRUD.HOOK.beforeToAdd]() {
      this.cid = ''
    },
    // 初始化编辑时候流程
    [CRUD.HOOK.beforeToEdit](crud, form) {
      this.cid = form.category
    },
    // 提交前做的操作
    [CRUD.HOOK.afterValidateCU](crud) {
      crud.form.category = this.cid
      return true
    },
    /**
     * 打开模型设计页面
     */
    designModel(row) {
      const url = this.activitiModelApi + '?modelId=' + row.id
      console.log(url)
      window.open(url)
    },
    /**
     * 部署流程
     */
    deploy(row) {
      crudActReModel
        .deploy(row.id)
        .then((res) => {
          this.crud.notify('部署成功', CRUD.NOTIFICATION_TYPE.SUCCESS)
        })
        .catch((err) => {
          console.log(err.response.data.message)
        })
    },
    /**
     * 下载流程模型文件
     */
    downloadBpmn(row) {
      crudActReModel.downloadBpmn(row.id).then((res) => {
        const url = window.URL.createObjectURL(res)
        var aLink = document.createElement('a')
        aLink.style.display = 'none'
        aLink.href = url
        aLink.setAttribute('download', row.name + '.bpmn20.xml')
        document.body.appendChild(aLink)
        aLink.click()
        document.body.removeChild(aLink)
        window.URL.revokeObjectURL(url)
      }).catch((err) => {
        console.log(err.response)
      })
    },
    /**
     * 获取流程分类数据
     */
    getCategorys() {
      crudProcessCategory.getAllCategorys().then(res => {
        this.categoryOptions = res
      }).catch(() => { })
    }
  }
}

</script>

<style scoped>
</style>
